#include "regx52.h"
#include "STC89C5xRC-rdp.h"
#include "datatype.h"
#include "bit.h"
#include "delay.h"

#include "uart.h"
#include "dht11.h"

#define LEN 5

uint_8 DHT11_data[LEN];
uint_8 celsius_symbol[3] = {0xA1, 0xE6, '\0'};

void show_data(void)
{
	send_msg("Humidity:");
	send_byte(DHT11_data[0]/10 + 0x30);
	send_byte(DHT11_data[0]%10 + 0x30);
	send_byte('.');
	send_byte(DHT11_data[1]/10 + 0x30);
	send_byte(DHT11_data[1]%10 + 0x30);
	send_msg("%rh\r\n");
	send_msg("Temperature:");
	send_byte(DHT11_data[2]/10 + 0x30);
	send_byte(DHT11_data[2]%10 + 0x30);
	send_byte('.');
	send_byte(DHT11_data[3]/10 + 0x30);
	send_byte(DHT11_data[3]%10 + 0x30);
	send_msg(celsius_symbol);
	send_msg("\r\n");
}

void setup(void)
{
	/* put your setup code here, to run once: */
	uart_init(BAUD_RATE_9600);
	RESET_BIT(ES);
	RESET_BIT(EA);
}

void main(void)
{
	setup();

	while (TRUE) {
		/* put your main code here, to run repeatedly: */
		delay_1s();
		read_DHT11_data(DHT11_data, LEN);
		if(!check_DHT11_data(DHT11_data, LEN))
			continue;
		show_data();
	}
}